package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallExpressTemplate;
import com.tencreat.service.mall.IMallExpressTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 快递模板Controller
 *
 * @author tencreat
 * @date 2022-01-06
 */
@Api(tags="快递模板api")
@RestController
@RequestMapping("/system/template")
public class AdminMallExpressTemplateController extends BaseController {
    @Autowired
    private IMallExpressTemplateService mallExpressTemplateService;
    /**
     * 查询快递模板列表
     */
    @ApiOperation(value="查询快递模板列表")
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallExpressTemplate>> list(@RequestBody  MallExpressTemplate mallExpressTemplate) {
        QueryWrapper<MallExpressTemplate> wrapper = Wrappers.query(mallExpressTemplate);
        if (SecurityUtils.getLoginUser().getUser().getUserId() != 1 && SecurityUtils.getLoginUser().getUser().getUserId() !=2) {
            wrapper.lambda().and(wrapper1 -> wrapper1.eq(MallExpressTemplate::getUserId, SecurityUtils.getLoginUser().getUser().getUserId()));
        }
        Page<MallExpressTemplate> page =  new Page<>(mallExpressTemplate.getPageNum(), mallExpressTemplate.getPageSize());
        return HttpResult.ok(mallExpressTemplateService.page(page, wrapper));
    }
    /**
     * 查询快递模板列表 不分页
     */
    @ApiOperation(value="查询快递模板列表不分页")
    @PostMapping("/allList")
    public HttpResult<List<MallExpressTemplate>> allList() {
        List<MallExpressTemplate> list = mallExpressTemplateService.lambdaQuery().list();
        return HttpResult.ok(list);
    }
    /**
     * 导出快递模板列表
     */
    @ApiOperation(value="导出快递模板列表")
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallExpressTemplate mallExpressTemplate) {
        List<MallExpressTemplate> list = mallExpressTemplateService.selectMallExpressTemplateList(mallExpressTemplate);
        ExcelUtil<MallExpressTemplate> util = new ExcelUtil<MallExpressTemplate>(MallExpressTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取快递模板详细信息
     */
    @ApiOperation(value="获取快递模板详细信息")
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallExpressTemplate> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallExpressTemplateService.selectMallExpressTemplateById(id));
    }

    /**
     * 新增快递模板
     */
    @ApiOperation(value="新增快递模板")
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallExpressTemplate mallExpressTemplate) {

        return to(mallExpressTemplateService.insertMallExpressTemplate(mallExpressTemplate));
    }

    /**
     * 修改快递模板
     */
    @ApiOperation(value="修改快递模板")
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallExpressTemplate mallExpressTemplate) {
        return to(mallExpressTemplateService.updateMallExpressTemplate(mallExpressTemplate));
    }

    /**
     * 删除快递模板
     */
    @ApiOperation(value="删除快递模板")
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallExpressTemplateService.deleteMallExpressTemplateByIds(ids));
    }
}
